package com.sky.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.exception.BaseException;
import com.sky.mapper.CategoryMapper;
import com.sky.result.PageResult;
import com.sky.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 菜品分类实现类
 *
 * @Author mr.lcc
 * @Data 2025/5/20 20:32
 */
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {

    @Autowired
    private CategoryMapper categoryMapper;

    /**
     * 分页查询
     */
    @Override
    public PageResult pageList(CategoryPageQueryDTO dto) {
        if (dto == null) {
            throw new BaseException("请求参数不能为空");
        }

        Page<Category> page = new Page<>(dto.getPage(), dto.getPageSize());

        this.lambdaQuery()
                .like(dto.getName() != null, Category::getName, dto.getName())
                .eq(dto.getType() != null, Category::getType, dto.getType())
                .orderByDesc(Category::getSort)
                .orderByDesc(Category::getCreateTime)
                .page(page);

        return new PageResult(page.getTotal(), page.getRecords());
    }
}
